//********************************************************
// Tile.java 
//Date: 4/30/2012
//Author: Andrew Landphier
//********************************************************
// GUI version of the tiles class. Used to set tile images and tile icons. 
//*******************************************************
//Edited: Jeff - Added buffered images
//*******************************************************

package ui;

import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import driver.Driver;
import functionality.Tiles;
import java.io.IOException;

public class Tile extends Tiles {
	public JLabel label;
	public ImageIcon icon;
	public String tland, tbuilding;
	private BufferedImage bF;
	
	/**
	 * Upon creating tile objects it creates associated superclass tiles data objects
	 * @param land string land type
	 * @param knightsReq minimum number of knights per tile
	 * @param building the build type
	 * @param points how many points the tile is worth
	 * @author Andrew Landphier
	 */
	public Tile(String land, int knightsReq, String building, int points) {
		super(land, knightsReq, building, points);
		matchAndSetImages(land, building);
		tland = land;
		tbuilding = building;
		
		
	}

	/**
	 * 
	 * @param t
	 */
	public Tile(Tiles t) {
		super(t);
	}	

	/**
	 * Matches the tile land type and tile building type and sets the appropriate tile image
	 * for the popup menu icons/tiles
	 * @param land string land type
	 * @param building string building type
	 * @author Andrew Landphier
	 */
	public void matchAndSetImages(String land, String building){
		if (land.equals(Driver.PLAIN)) {
			if (building.equals(Driver.VILLAGE)){
				setPlain_village();
			}
			if (building.equals(Driver.TOWN)) {
				setPlain_town();
			}			
			if (building.equals(Driver.CASTLE)) {
				setPlain_castle();
			}
			if (building.equals(Driver.NONE)) {
				setPlain();
			}
		} else if (land.equals(Driver.FOREST)){
			if (building == Driver.VILLAGE) {
				setForest_village();
			}
			if (building.equals(Driver.TOWN)) {
				setForest_town();
			}
			if (building.equals(Driver.CASTLE)) {
				setForest_castle();
			}
			
		} else if (land.equals(Driver.MOUNTAIN)){
			setMountain();
		} else if (land.equals(Driver.LAKE)) {
			setLake();
		}
	}

	
	

	/********************* SETTERS ******************************************
	 
	/**
	 * Sets the tile label
	 * @param l
	 * @author Andrew Landphier
	 */
	private void setLabel(JLabel l){
		label = l;
	}

	/**
	 * sets the icon image for the popup menu
	 * @param i
	 * @author Andrew Landphier
	 */
	private void setIcon(ImageIcon i){
		icon = i;
	}
	
	/**
	 * sets the buffered image for the overlay
	 * @param bf
	 * @author Jeff
	 */
	private void setBufferedImage(BufferedImage bf){
		bF = bf;
	}
	
	
	
	/********************* GETTERS ******************************************
	/**
	 * Returns the label associated with the tile
	 * @return JLabel set tile label
	 */
	public JLabel getLabel(){
		return label;
	}
	
	/**
	 * returns the icon image of the tile
	 * @return ImageIcon 
	 * @author Andrew Landphier
	 */
	public ImageIcon getIcon() {
		return icon;
	}
	
	/**
	 * Returns a buffered image of the tile
	 * @return buffered image
	 * @author Andrew Landphier
	 */
	public BufferedImage getBufferedImage()
	{
		return bF;
	}
	
	
	// *********************** Helper methods for creating the label objects *****************
	/**
	 * sets the icon/label/ buffered image to lake
	 * @author Andrew Landphier
	 */
	private void setLake(){
		try {
			BufferedImage lake = ImageIO.read(new File("ICONS/lake_none.jpg"));
			setLabel(new JLabel(new ImageIcon(lake)));
			setBufferedImage(lake);
			setIcon(new ImageIcon("ICONS.32X32/lake_none.jpg"));
		} catch (IOException e){
			System.out.println("Lake not found: " + e.getMessage());
		}
	}

	/**
	 * sets the icon/label/buffered image to plain none
	 * @author Andrew Landphier
	 */
	private JLabel setPlain(){
		JLabel tlabel = new JLabel();
		try {
			BufferedImage plain = ImageIO.read(new File("ICONS/plain_none.jpg"));
			setLabel(new JLabel(new ImageIcon(plain)));
			setBufferedImage(plain);
			setIcon(new ImageIcon("ICONS.32X32/plain_none.jpg"));
		} catch (IOException e){
			System.out.println("Plain not found: " + e.getMessage());
		}
		return tlabel;
	}

	/**
	 * sets the icon/label/buffered image to plain village
	 * @author Andrew Landphier
	 */
	private void setPlain_village(){
		try {
			BufferedImage plain_village = ImageIO.read(new File("ICONS/plain_village.jpg"));
			setLabel(new JLabel(new ImageIcon(plain_village)));
			setBufferedImage(plain_village);
			setIcon(new ImageIcon("ICONS.32X32/plain_village.jpg"));
		} catch (IOException e){
			System.out.println("plain village not found: " + e.getMessage());
		}
	}	

	/**
	 * sets the icon/label/buffered image to plain town
	 * @author Andrew Landphier
	 */
	private void setPlain_town(){
		try {
			BufferedImage plain_town = ImageIO.read(new File("ICONS/plain_town.jpg"));
			setLabel(new JLabel(new ImageIcon(plain_town)));
			setBufferedImage(plain_town);
			setIcon(new ImageIcon("ICONS.32X32/plain_town.jpg"));
		} catch (IOException e){
			System.out.println("Plain town not found: " + e.getMessage());
		}
	}	

	/**
	 * Sets the icon/label/buffered image to forest castle
	 * @author Andrew Landphier
	 */
	private void setPlain_castle(){
		try {
			BufferedImage plain_castle = ImageIO.read(new File("ICONS/plain_castle.jpg"));
			setLabel(new JLabel(new ImageIcon(plain_castle)));
			setBufferedImage(plain_castle);
			setIcon(new ImageIcon("ICONS.32X32/plain_castle.jpg"));
		} catch (IOException e){
			System.out.println("Plain castle not found: " + e.getMessage());
		}
	}	

	/**
	 * Sets the icon/label/buffered image to forest castle
	 * @author Andrew Landphier
	 */
	private void setForest_castle(){
		try {
			BufferedImage forest = ImageIO.read(new File("ICONS/forest_castle.jpg"));
			setLabel(new JLabel(new ImageIcon(forest)));
			setBufferedImage(forest);
			setIcon(new ImageIcon("ICONS.32X32/forest_castle.jpg"));
		} catch (IOException e){
			System.out.println("Forest castle not found: " + e.getMessage());
		}
	}

	/**
	 * Sets the icon/label/buffered image to forest town
	 * @author Andrew Landphier
	 */
	private void setForest_town(){
		try {
			BufferedImage forest = ImageIO.read(new File("ICONS/forest_town.jpg"));
			setLabel( new JLabel(new ImageIcon(forest)));
			setBufferedImage(forest);
			setIcon(new ImageIcon("ICONS.32X32/forest_town.jpg"));
		} catch (IOException e){
			System.out.println("Forest town not found: " + e.getMessage());
		}
	}

	/**
	 * Sets the icon/label/buffered image to forest village
	 * @author Andrew Landphier
	 */
	private void setForest_village(){
		try {
			BufferedImage forest = ImageIO.read(new File("ICONS/forest_village.jpg"));
			setLabel(new JLabel(new ImageIcon(forest)));
			setBufferedImage(forest);
			setIcon(new ImageIcon("ICONS.32X32/forest_village.jpg"));
		} catch (IOException e){
			System.out.println("Forest village not found: " + e.getMessage());
		}
	}
	
	/**
	 * Sets the Icon/Label/Buffered image to mountain
	 * @author Andrew Landphier
	 */
	private void setMountain(){
		try {
			BufferedImage mountain = ImageIO.read(new File("ICONS/mountain_none.jpg"));
			setLabel(new JLabel(new ImageIcon(mountain)));
			setBufferedImage(mountain);
			setIcon(new ImageIcon("ICONS.32X32/mountain_none.jpg"));
		} catch (IOException e){
			System.out.println("Mountain not found: " + e.getMessage());
		}
	}
	
}